package com.example.treeutil.controller;

import com.example.treeutil.layer.MuLu;
import com.example.treeutil.layer.ResourceLayer;
import com.example.treeutil.layer.TreeNode;
import com.example.treeutil.model.Catalog;
import com.example.treeutil.model.anno.Apple;
import com.example.treeutil.model.anno.Directory;
import com.example.treeutil.model.anno.蛋糕;
import com.example.treeutil.model.layer.Layer;
import com.example.treeutil.model.layer.WFS;
import com.example.treeutil.model.layer.WMTS;
import com.example.treeutil.tree.Node;
import com.example.treeutil.tree.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

/**
 * 2022/12/9
 *
 * @author fengyu
 */
@Api(tags = "获取目录树")
@RestController
@RequestMapping("/")
public class ResController {

    @ApiOperation("示例1：获取单一对象组成的目录树")
    @GetMapping("c")
    public ResponseEntity<List<Catalog>> getCatalogs(){
        List<Catalog> catalogList = new ArrayList<>();
        // 如构建以下结构的目录树
        catalogList.add(new Catalog("1", "-1", "mulu--1", 0));
            catalogList.add(new Catalog("1-3", "1", "mulu--1--2", 2));
            catalogList.add(new Catalog("1-4", "1", "mulu--1--3", 3));
            catalogList.add(new Catalog("1-5", "1", "mulu--1--1", 1));
        catalogList.add(new Catalog("2", "-1", "mulu--2", 1));
            catalogList.add(new Catalog("2-6", "2", "mulu--2--2", 2));
                catalogList.add(new Catalog("2-6-9", "2-6", "mulu--2--2--1", 3));
                catalogList.add(new Catalog("2-6-10", "2-6", "mulu--2--2--2", 1));
                catalogList.add(new Catalog("2-6-10", "2-6", "mulu--2--2--3", 2));
                    catalogList.add(new Catalog("2-6-10-1", "2-6-10", "mulu--2--2--3-1", 1));
            catalogList.add(new Catalog("2-7", "2", "mulu--2--3", 3));
            catalogList.add(new Catalog("2-8", "2", "mulu--2--1", 1));


        return ResponseEntity.ok(new TreeUtil<Catalog, String>().toTree(
                catalogList,
                Catalog::getId,
                Catalog::getPid,
                catalog -> catalog.getPid() != "-1",
                (c1, c2) -> c1.getSubs().add(c2),
                Comparator.comparingInt(Catalog::getIndex),
                Catalog::getSubs
        ).get());
    }

    /**
     * 不同类型的对象组织到一个目录树中，需要各个对象实现 Node 接口*
     */
    @ApiOperation("示例2：Node接口方式获取资源目录树")
    @GetMapping("/c2")
    public ResponseEntity<List<Node>> getByNode(){
        List<Node> nodeList = new ArrayList<>();
        // 先构建两级目录
        nodeList.add(new Layer("1", "", "道路", 0));
        nodeList.add(new Layer("2", "", "水系", 1));
            nodeList.add(new Layer("3", "2", "河流", 0));
            nodeList.add(new Layer("4", "2", "水库", 0));
        nodeList.add(new Layer("5", "", "历史影像图", 2));

        // 生成目录下的具体资源
        nodeList.add(new WMTS("d1", "1", "主干道", "http://a.b.c", new Date(), "mc"));
        nodeList.add(new WMTS("d2", "1", "乡道", "http://a.b.c", new Date(), "mc"));
        nodeList.add(new WFS("D3", "1", "省道", "http://a.b.c", new Date(), "4490", "PNG"));
        nodeList.add(new WFS("s3", "3", "渭河", "http://a.b.c", new Date(), "4490", "PNG"));
        nodeList.add(new WMTS("s2", "4", "新安江水库", "http://a.b.c", new Date(), "mc"));
        nodeList.add(new WFS("s4", "4", "石砭峪水库", "http://a.b.c", new Date(), "4490", "PNG"));

        nodeList.add(new WMTS("y2", "5", "2018年影像图", "http://a.b.c", Date.from(LocalDate.of(2018,10,11).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()), "mc"));
        nodeList.add(new WMTS("yq", "5", "2013年影像图", "http://a.b.c", Date.from(LocalDate.of(2013,10,11).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()), "mc"));
        nodeList.add(new WMTS("y42", "5", "2007年影像图", "http://a.b.c", Date.from(LocalDate.of(2007,10,11).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()), "mc"));
        nodeList.add(new WMTS("y22", "5", "2021年影像图", "http://a.b.c", Date.from(LocalDate.of(2021,10,11).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()), "mc"));

        return ResponseEntity.ok(TreeUtil.toTree(nodeList).get());
    }

    @ApiOperation("示例3：注解方式组织成目录树")
    @GetMapping("c3")
    public ResponseEntity<List<Object>> getByAnno(){
        List<Object> list = new ArrayList<>();
        // 目录
        list.add(new Directory("1", null, "糕点", 0));
        list.add(new Directory("2", null, "水果", 0));
            list.add(new Directory("3", "2", "苹果", 0));
            list.add(new Directory("4", "2", "其他", 0));

        list.add(new Apple("aa", "3", "富士", "陕西洛川", 0));
        list.add(new Apple("a1", "3", "黄元帅", "陕西蒲城", 0));
        list.add(new Apple("ae", "4", "石榴", "陕西西安", 0));
        list.add(new 蛋糕("da", "1", "甜甜圈", 2.1, "一天时间"));
        list.add(new 蛋糕("db", "1", "切片面包", 7.0, "十天时间"));
        return ResponseEntity.ok(TreeUtil.toTreeByNodeTile(list).get());
    }

    private List<com.example.treeutil.datasource.WMTS> createWMTS() {
        String[] names = {"img2015年","18img","22img","yhemap","bou",
                "img2022","map_house","ewr","ttp","lping","emap"};
        List<com.example.treeutil.datasource.WMTS> wmtsList = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            com.example.treeutil.datasource.WMTS wmts = new com.example.treeutil.datasource.WMTS();
            wmts.setId(i);
            wmts.setName(names[i]);
            wmtsList.add(wmts);
        }
        return wmtsList;
    }

    @ApiOperation("示例4：自定义接口对象获取目录树")
    @GetMapping("c4")
    public ResponseEntity<List<TreeNode>> getAllLayers() {
        String[] names = {"15年影像","18年影像","22年影像","旅游热点图","热门小区",
                "21年影像","在建工地","学区划片","农贸市场","23年地形图","23年影像"};
        List<MuLu> catelogs = new ArrayList<>();
        MuLu d1 = new MuLu();
        d1.setId(3+"c");
        d1.setName("顶层目录");
        d1.setIndex(1);
        d1.setParent(null);
        catelogs.add(d1);

        MuLu c1 = new MuLu();
        c1.setId(1+"c");
        c1.setName("第二层专题A");
        c1.setIndex(2);
        catelogs.add(c1);
        c1.setParent(d1);

        MuLu c2 = new MuLu();
        c2.setParent(d1);
        c2.setId(2+"c");
        c2.setName("第二层目录E");
        c2.setIndex(1);
        catelogs.add(c2);

        List<com.example.treeutil.datasource.WMTS> wmtsList = createWMTS();

        MuLu e1 = new MuLu();
        e1.setId(5+"c");
        e1.setName("目录E的子目录77");
        e1.setIndex(1);
        e1.setParent(c2);
        catelogs.add(e1);


        Random random = new Random();
        List<ResourceLayer> resourceLayers = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            ResourceLayer resourceLayer = new ResourceLayer();
            resourceLayer.setId(i);
            resourceLayer.setIndex(random.nextInt(10));
            resourceLayer.setTitle(names[i]);
            resourceLayer.setLayer(wmtsList.get(i));
            resourceLayer.setCatelog(random.nextBoolean() ? c1 : (random.nextBoolean() ? c2 : e1));
            resourceLayers.add(resourceLayer);
        }

        List<TreeNode> treeNodeList = new ArrayList<>();
        treeNodeList.addAll(catelogs);
        treeNodeList.addAll(resourceLayers);

        return  ResponseEntity.ok(TreeUtil.toTree2(treeNodeList,
                TreeNode::getId,
                TreeNode::getPId,
                TreeNode::hasParent,
                TreeNode::addChild,
                Comparator.comparingInt(TreeNode::getIndex),
                TreeNode::getSubs).get()
        );
    }
}
